&& (Logical AND)

The Logical AND operator is used to perform the Boolean AND operation on two separate expressions, which are given each side of the operator. It differs from the Bitwise AND (&) operator in that whole expressions are evaluated instead of individual numbers, though they both follow the same set of simple rules, as follows.

• A One (true) and a Zero (false), evaluated, returns a zero (false).

• A Zero (false) and a One (true), evaluated, returns a zero (false).

• A Zero (false) and a Zero (false), evaluated, returns a zero (false).

• A One (true) and a One (true), evaluated, returns a one (true).

Following the above set of rules, if the left expression evaluates to false, the right expression is never evaluated. If the left expression evaluates to true, then the right expression is evaluated. The product of the two expressions as a whole are then evaluated, and the result is returned. It should be noted that due to the way the Logical AND operator works, the product of the right expression is always the result which is returned.

syntax:

expressionOne && expressionTwo

EXAMPLE

variableOne = 50;

if ( (50 == variableOne) && (variableOne == 30) ) {

document.write("The Logical AND operation is True");

} else {

document.write("The Logical AND operation is False");

}

The above example declares one variable, named variableOne, which has been loaded with the number 50. The Logical AND operator is then used in an if / else statement. Since the left expression evaluates to true (the contents of variableOne is 50), the right expression is evaluated. Since the right expression is false (the contents of variableOne are not 30), the result returned is false. The false document.write statement is then written to the screen.